package automatchingview;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.zx.theline.R;

import java.util.ArrayList;
import java.util.List;

import Utils.ScreenUtils;
import app.MyApplication;
import greendao.UserDao;
import greendao.entity.User;
import views.AutoMatchingPopuClickListener;
import views.AutoMatchingPopuWindowDate;

/**
 * Created by Ren on 2018/9/27.
 */

public class AutoMatchingPopuWindow<T> implements View.OnClickListener {


    View view;

    private PopupWindow popupWindow;

    private EditText pop_edit;

    TextView checked_tv, cancel, confirm;

    private ListView listView;

    private Adapter adapter;

    private Context mContext;

    private UserDao mUserDao;

    private boolean flag;

    //数据源
    private List<T> mSourceList =new ArrayList<>();

    //筛选后数据源
    private List<T> afterList = new ArrayList<>();

    //选中的数据
    private List<T> checkedList = new ArrayList<>();

    //是否单选 默认单选
    private Boolean mSingleElection = true;

    private Boolean mOutsideTouchable = false;

    public AutoMatchingPopuClickListener<T> mAutoMatchingPopuClickListener;

    public TextWatcher mTextWatcher;

    public PopupWindow CreatePopu() {

        view = LayoutInflater.from(mContext).inflate(R.layout.popu_automatching, null);
        popupWindow = new PopupWindow(view, 2 * ScreenUtils.getScreenWidth(mContext) / 3, ViewGroup.LayoutParams.MATCH_PARENT, true);
        ColorDrawable cd = new ColorDrawable(Color.WHITE);
        popupWindow.setBackgroundDrawable(cd);
        popupWindow.setOutsideTouchable(mOutsideTouchable);

        initView();
        initListView();
        AutoMatching();
        return popupWindow;
    }

    public void initView() {
        pop_edit = (EditText) view.findViewById(R.id.pop_edit);
        checked_tv = (TextView) view.findViewById(R.id.checked_tv);
        cancel = (TextView) view.findViewById(R.id.cancel);
        confirm = (TextView) view.findViewById(R.id.confirm);
        listView = (ListView) view.findViewById(R.id.listView);
        if(flag){
            pop_edit.setVisibility(View.GONE);
        }

        cancel.setOnClickListener(this);
        confirm.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                popupWindow.dismiss();
                break;
            case R.id.confirm:
                mAutoMatchingPopuClickListener.OnAutoMatchingPopClicked(checkedList);
                break;
        }
    }

    public void initListView() {
        mUserDao = MyApplication.getDaoSession().getUserDao();
        afterList = mSourceList;
        adapter = new Adapter();
        listView.setAdapter(adapter);
    }

    public void AutoMatching() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                单选模式
//                if (mSingleElection) {
//                    if (((AutoMatchingPopuWindowDate) (afterList.get(position))).isChecked()) {
//                        ((AutoMatchingPopuWindowDate) (afterList.get(position))).setChecked(!((AutoMatchingPopuWindowDate) (afterList.get(position))).isChecked());
//                        checkedList.clear();
//                    } else {
//                        ((AutoMatchingPopuWindowDate) (afterList.get(position))).setChecked(!((AutoMatchingPopuWindowDate) (afterList.get(position))).isChecked());
//                        checkedList.add(afterList.get(position));
//                    }
//                    adapter.notifyDataSetChanged();
//                } else {
//                多选模式
                AutoMatchingPopuWindowDate date= (AutoMatchingPopuWindowDate) afterList.get(position);
                    if (date.isChecked()) {
                        date.setChecked(!date.isChecked());
                        checkedList.remove(afterList.get(position));
                    } else {
                        date.setChecked(!date.isChecked());
                        checkedList.add(afterList.get(position));
                    }
                    adapter.notifyDataSetChanged();
                    checked_tv.setText(checkedList.toString());
//                }
            }
        });
        pop_edit.addTextChangedListener(mTextWatcher);
    }

    public AutoMatchingPopuWindow setAutoMatchingPopuClickListener(AutoMatchingPopuClickListener autoMatchingPopuClickListener) {
        this.mAutoMatchingPopuClickListener = autoMatchingPopuClickListener;
        return this;
    }

    public AutoMatchingPopuWindow setEditTextWatcher(TextWatcher TextWatcher) {
        this.mTextWatcher = TextWatcher;
        return this;
    }

    public AutoMatchingPopuWindow Builder(Context context) {
        this.mContext = context;
        return this;
    }

    //是否单选
    public AutoMatchingPopuWindow setAutoMatchingPopuWindowSingleElection(boolean singleElection) {
        this.mSingleElection = singleElection;
        return this;
    }

    public AutoMatchingPopuWindow setOutsideTouchable(boolean outsideTouchable) {
        this.mOutsideTouchable = outsideTouchable;
        return this;
    }

    public AutoMatchingPopuWindow setSourceList(List<T> list) {
        this.mSourceList = list;
        return this;
    }

    public AutoMatchingPopuWindow setCheckedList(List<T> list) {
        this.checkedList = list;
        return this;
    }

    public AutoMatchingPopuWindow hideText(boolean flag) {
        this.flag = flag;
        return this;
    }




    public void UpListViewAdapter(List<T> list) {
        afterList = list;
        adapter.notifyDataSetChanged();
    }


    class Adapter<T> extends BaseAdapter {

        @Override
        public int getCount() {
            return afterList.size();
        }

        @Override
        public T getItem(int position) {
            return (T) afterList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_popu_automatching, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.tv = (TextView) convertView.findViewById(R.id.tv);
                viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
                viewHolder.linearLayout = (LinearLayout) convertView.findViewById(R.id.linearlayout);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            final AutoMatchingPopuWindowDate date = (AutoMatchingPopuWindowDate) afterList.get(position);
            viewHolder.tv.setText(date.getString());
            if (date.isChecked()) {
                viewHolder.checkBox.setChecked(true);
            } else {
                viewHolder.checkBox.setChecked(false);
            }
            //单选模式
//            if (mSingleElection) {

//                viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                    @Override
//                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                        if (isChecked) {
//                            ((AutoMatchingPopuWindowDate)(afterList.get(position))).setChecked(true);
//                            checkedList.clear();
//                            checkedList.add(afterList.get(position));
//                        } else {
//                            ((AutoMatchingPopuWindowDate)(afterList.get(position))).setChecked(false);
//                        }
//                    }
//                });
//            } else {
            //多选模式

//                viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                    @Override
//                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                        if (isChecked) {
//                            ((AutoMatchingPopuWindowDate)(afterList.get(position))).setChecked(true);
//                            checkedList.add(afterList.get(position));
//                        } else {
//                            ((AutoMatchingPopuWindowDate)(afterList.get(position))).setChecked(false);
//                            checkedList.remove(afterList.get(position));
//                        }
//                    }
//                });
//            }

            return convertView;
        }

        class ViewHolder {
            TextView tv;
            CheckBox checkBox;
            LinearLayout linearLayout;
        }
    }

}
